<?php

/**
 * Yii Controller MenuController
 *
 * @author <KTD SOFT>
 * @since Jan 31, 2013 - 12:11:05 AM
 */
class MenuController extends BackController
{

    public $modelName     = 'Menu';
    public $parent        = NULL;
    public $fieldSearch   = 'name';
    public $maxPageButton = 5;

    public function actionIndex()
    {
        $cri             = new CDbCriteria();
        $cri->condition  = 'language_id=:l';
        $cri->params     = array(
            ':l' => Yii::app()->controller->lang_id,
        );
        $cri->order      = 'rank ASC';
        $count           = Menu::model()->count($cri);
        $pages           = new CPagination($count);
        $pages->route    = 'menu/index';
        $pages->pageSize = 10;
        $pages->applyLimit($cri);
        $model           = Menu::model()->findAll($cri);
        $this->render('index', array(
            'model' => $model,
            'pages' => $pages,
        ));
    }

    public function save($action = 'Create')
    {
        $status = parent::save($action);

        if (isset($_POST['MenuItem']) && $_POST != null)
        {
            $status = FALSE;
            if (isset($_GET['parent']))
            {
                $item = MenuItem::model()->findByPk($_GET['parent']);
            }
            else
            {
                $item = new MenuItem();
            }
            $item->attributes = @$_POST['MenuItem'];
            $item->menu_id    = $this->model->id;
            if (!$item->validate())
            {
                $noti = CHtml::errorSummary($item);
                Yii::app()->user->setFlash('noti-form', $noti);
            }
            else
            {
                if ($status = $item->save())
                {
                    $this->redirect(Yii::app()->controller->createUrl('menu/update', array(
                                'lang' => Yii::app()->language,
                                'pk'   => $this->model->id,
                    )));
                }
            }
        }
        return $status;
    }

    public function init()
    {
        parent::init();
        $this->controllerMenu   = array();
        $this->controllerMenu[] = array(
            'label' => 'Cài đặt chung',
            'url'   => $this->createUrl('option/index', array(
                'lang' => $this->lang,
            ))
        );
        $this->controllerMenu[] = array(
            'label' => 'Cài đặt menu',
            'url'   => $this->createUrl('Menu/index', array('lang' => $this->lang))
        );
        $this->controllerMenu[] = array(
            'label' => Yii::t('option', 'Media'),
            'url'   => $this->createUrl('option/media', array('lang' => $this->lang))
        );
    }

    public function actionDeleteItems()
    {
        // Thông báo nếu cập nhật thành công
        if (isset($_GET['deleteItem']))
        {
            $id = $_GET['deleteItem'];
            if (MenuItem::model()->deleteByPk($id))
            {
                $noti = 'Bạn đã xóa thánh công';
                Yii::app()->user->setFlash('noti', $noti);
            }
            $this->redirect(Yii::app()->controller->createUrl('menu/update', array(
                        'lang' => Yii::app()->language,
                        'pk'   => $this->model->id,
            )));
        }
    }

    protected function form($action = 'Create')
    {
        if (isset($_GET['parent']))
        {
            $id           = $_GET['parent'];
            $this->parent = MenuItem::model()->findByPk($id);
        }
        else
        {
            $this->parent = null;
        }

        parent::form($action);
    }

    public function actionAjaxLoadDataByType()
    {

        $type = $_POST['type'];

        $data = array();
        switch ($type)
        {
            case MenuItem::TYPE_CATEGORY:
                $data = Category::getPublicMenuArray();
                break;
            case MenuItem::TYPE_POST:
                $data = Post::getArrayPost();
                break;
            case MenuItem::TYPE_CONTROLLER:
                $data = Menu::getPageLink();
                break;
        }
        $menu = MenuItem::model()->findByPk($_POST['item_id']);
        echo $this->renderPartial('_select_data', array(
            'data' => $data,
            'menu' => $menu
        ));
    }

}

/** 
 * End file SupporterControllers
 */
